package com.blog.dazi.utils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class RandomDataGenerator {
    public static void main(String[] args) {
        Map<String, Object> userData = generateUserData();
        System.out.println(userData);
    }

    public static Map<String, Object> generateUserData() {
        Map<String, Object> userData = new HashMap<>();

        // 生成手机号
        String phoneNumber = generatePhoneNumber();
        userData.put("phone_number", phoneNumber);

        // 生成用户名
        String username = generateChineseName();
        userData.put("username", username);

        // 生成所在县级地址
//        String address = generateChineseAddress();
//        userData.put("localtion", address);

        // 生成县级地址坐标
//        double latitude = generateLatitude();
//        double longitude = generateLongitude();
//        Map<String, Double> coordinates = new HashMap<>();
//
//        userData.put("coordinate", latitude+","+longitude);

        // 生成性别
        String gender = generateGender();
        userData.put("sex", gender);

        // 生成11位随机数
        long randomNumber = generateRandomNumber();
        userData.put("dazi_number", randomNumber);

        // 生成图片
        String image = generateRandomImage();
        userData.put("photo", image);
        userData.put("user_type", 0);

        return userData;
    }

    public static String generatePhoneNumber() {
        StringBuilder phoneNumber = new StringBuilder("1");
        Random random = new Random();

        // 生成前三位区号
        int[] areaCode = {3, 5, 7, 8};
        int randomIndex = random.nextInt(areaCode.length);
        phoneNumber.append(areaCode[randomIndex]);

        // 生成后面的7位号码
        for (int i = 0; i < 9; i++) {
            phoneNumber.append(random.nextInt(10));
        }

        return phoneNumber.toString();
    }

    public static String generateChineseName() {
        String[] surnames = {"王", "李", "张", "刘", "陈", "杨", "黄", "吴", "周", "徐", "孙", "马", "朱", "胡", "郭", "何", "高", "林", "罗", "郑", "梁", "谢", "宋", "唐", "许", "韩", "冯", "邓", "曹", "彭", "曾", "肖", "田", "董", "袁", "于", "余", "叶", "蒋", "杜", "苏", "魏", "蔡", "贾", "丁", "薛", "严", "尹", "施", "牛", "洪", "龚", "程", "嵇", "邢", "滕", "靳", "侯", "解", "雷", "龙", "柴", "卫", "邹", "喻", "柏", "窦", "苗", "宁", "鲍", "葛", "闫", "司马", "上官", "欧阳", "夏侯", "诸葛", "东方", "尉迟", "公孙", "长孙", "慕容", "司徒", "司空", "端木", "颛孙", "百里", "赫连", "皇甫", "呼延", "南宫", "荀彧", "后羿", "轩辕", "令狐", "左丘", "公良", "段干", "百里", "东郭", "南门", "呼延", "羊舌", "微生", "梁丘", "西门", "公冶", "太叔", "公仲", "宗政", "濮阳", "公祖", "公乘", "壤驷", "公西", "都智", "公羊", "赫连", "漆雕", "乐正", "谷梁", "拓跋", "夹谷", "车非", "壤驷"};

        String[] givenNames = {"大", "二", "三", "四", "五", "六", "七", "八", "九", "十", "小", "中", "无",
                "明", "红", "华", "丽", "建", "国", "强", "宝", "荣", "力", "富", "贵", "庆",
                "栋", "伟", "胜", "盛", "海", "超", "良", "康", "瑞", "安", "亮", "风", "梁",
                "奇", "雄", "鹏", "勇", "毅", "俊", "锦", "涛", "春", "翔", "阳", "震", "宇",
                "凡", "浩", "琪", "晶", "欣", "可", "思", "雨", "婷", "雪", "倩", "蓝", "菲",
                "佳", "玉", "秀", "华", "文", "静", "敏", "雅", "芳", "颖", "岚", "娜", "丹",
                "琳", "莉", "慧", "娟", "洁", "丽", "珍", "怡", "美", "宜", "兰", "秋", "荷"};
        Random random = new Random();
        int i = random.nextInt(surnames.length);
        String surname = surnames[i];
        String givenName = givenNames[random.nextInt(givenNames.length)];
        if(i%2==0){
            givenName += givenNames[random.nextInt(givenNames.length)];
        }

        return surname + givenName;
    }

    public static String generateChineseAddress() {
        String[] provinces = {"北京市", "上海市", "天津市", "重庆市", "河北省", "山西省", "辽宁省", "吉林省", "黑龙江省", "江苏省", "浙江省", "安徽省", "福建省", "江西省", "山东省", "河南省", "湖北省", "湖南省", "广东省", "海南省", "四川省", "贵州省", "云南省", "陕西省", "甘肃省", "青海省", "台湾省", "内蒙古自治区", "广西壮族自治区", "西藏自治区", "宁夏回族自治区", "新疆维吾尔自治区", "香港特别行政区", "澳门特别行政区"};
        String[] cities = {"北京市", "上海市", "天津市", "重庆市", "石家庄市", "唐山市", "秦皇岛市", "邯郸市", "邢台市", "保定市", "张家口市", "承德市", "沧州市", "廊坊市", "衡水市", "太原市", "大同市", "阳泉市", "长治市", "晋城市", "朔州市", "晋中市", "运城市", "忻州市", "临汾市", "吕梁市", "沈阳市", "大连市", "鞍山市", "抚顺市", "本溪市", "丹东市", "锦州市", "营口市", "阜新市", "辽阳市", "盘锦市", "铁岭市", "朝阳市", "葫芦岛市", "长春市", "吉林市", "四平市", "辽源市", "通化市", "白山市", "松原市", "白城市", "延边朝鲜族自治州", "哈尔滨市", "齐齐哈尔市", "鸡西市", "鹤岗市", "双鸭山市", "大庆市", "伊春市", "佳木斯市", "七台河市", "牡丹江市", "黑河市", "绥化市", "大兴安岭地区"};
        String[] counties = {"县1", "县2", "县3", "县4", "县5", "县6", "县7", "县8", "县9", "县10", "县11", "县12", "县13", "县14", "县15", "县16", "县17", "县18", "县19", "县20", "县21", "县22", "县23", "县24", "县25", "县26", "县27", "县28", "县29", "县30"};
        Random random = new Random();
        String province = provinces[random.nextInt(provinces.length)];
        String city = cities[random.nextInt(cities.length)];
        String county = counties[random.nextInt(counties.length)];
        return province + city + county;
    }

    public static double generateLatitude() {
        // 假设纬度范围在 -90 到 90 之间
        Random random = new Random();
        return random.nextDouble() * 180 - 90;
    }

    public static double generateLongitude() {
        // 假设经度范围在 -180 到 180 之间
        Random random = new Random();
        return random.nextDouble() * 360 - 180;
    }

    public static String generateGender() {
        String[] genders = {"男", "女"};
        Random random = new Random();
        return genders[random.nextInt(genders.length)];
    }

    public static long generateRandomNumber() {
        Random random = new Random();
        return (long) (random.nextDouble() * 900000000L) + 100000000L;
    }

    public static String generateRandomImage() {
        int width = 100; // 图片宽度
        int height = 100; // 图片高度

        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = image.createGraphics();

        // 填充随机颜色背景
        Random random = new Random();
//        Color backgroundColor = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
        Color backgroundColor = new Color(50,50,150);
        graphics.setColor(backgroundColor);
        graphics.fillRect(0, 0, width, height);

        // 绘制随机文本
//        String randomText = generateRandomText(6); // 随机生成6位文本

        Font font = new Font("宋体", Font.BOLD, 24);
        graphics.setFont(font);
        graphics.setColor(Color.gray);
        graphics.drawString("搭子", 25, 60);

        // 生成Base64字符串
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            ImageIO.write(image, "png", baos);
            byte[] imageBytes = baos.toByteArray();
            return Base64.getEncoder().encodeToString(imageBytes);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                baos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return null;
    }

    public static String generateRandomText(int length) {
        String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        StringBuilder randomText = new StringBuilder(length);
        Random random = new Random();

        for (int i = 0; i < length; i++) {
            int index = random.nextInt(characters.length());
            char randomChar = characters.charAt(index);
            randomText.append(randomChar);
        }

        return randomText.toString();
    }



    public static String generateImage(String content) {
        int width = 370; // 图片宽度
        int height = 200; // 图片高度

        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = image.createGraphics();

        // 填充随机颜色背景
        Random random = new Random();
        Color backgroundColor = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
        graphics.setColor(backgroundColor);
        graphics.fillRect(0, 0, width, height);

        // 绘制随机文本
//        String randomText = generateRandomText(6); // 随机生成6位文本

        Font font = new Font("宋体", Font.BOLD, 24);
        graphics.setFont(font);
        graphics.setColor(Color.WHITE);
        String[] split = content.split("\n");
        for (int i = 0; i < split.length; i++) {
            graphics.drawString(split[i], 50, 50+i*39);
        }


        // 生成Base64字符串
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            ImageIO.write(image, "png", baos);
            byte[] imageBytes = baos.toByteArray();
            return Base64.getEncoder().encodeToString(imageBytes);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                baos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return null;
    }
}